<?php

namespace App\Http\Resources\Api;

use App\Models\IntegralUser;
use App\Support\ResourceCollection;

class Integrals extends ResourceCollection
{
    protected $userId;

    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $this->userId = app('auth')->id();
        return $this->collection->map([$this, 'item'])->all();
    }

    /**
     * 格式化单个数据
     *
     * @param \Modules\Xiaoyu\Models\Banner $item
     * @return array
     */
    public function item($item)
    {
        $item->full_image = image_url($item->image);

        $item = $item->toArray();

        if (empty($item['user'])) {
            $item['user'] = IntegralUser::getDefaultUser($this->userId, $item['id']);
        }
        $item['is_locked'] = $this->collection[0]->id == $item['id'] || $item['user']['prize_count'] >= $item['upgrade_need'] ? false : true;

        return $item;
    }
}
